#include <QApplication>
#include <QTranslator>
#include <QLibraryInfo>
#include <QTextCodec>
#include <QLocale>
#include <QDebug>
#include <QDir>
#include <QSplashScreen>
#include <ppjtools.h>
#include <QString>
//#include <QGtkStyle>
#include <QtGui>
#include <QTableWidget>

#include "config.h"
#include "widget.h"


void initDirs ();
void test ()
{
    QGridLayout gl;
//    gl.
}

//--------------------------------
int main (int argc, char *argv[])
{
    //	****************************
    //	* Initialisations importantes *
    //	****************************

    QApplication::setDesktopSettingsAware (true);

    QApplication::setQuitOnLastWindowClosed (false);

    QApplication app (argc, argv);

    QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
    QSplashScreen splash (QPixmap (":/images/splash.png"));

    splash.show ();
    QCoreApplication::processEvents ();

    app.setOrganizationName ("PapaJaac");
    app.setApplicationName (Config::appName ());
    app.setApplicationVersion (Config::appVersion ());
    app.setWindowIcon (Config::appIcon ());

#if (QT_VERSION < 0x050000)
    QTextCodec::setCodecForTr (QTextCodec::codecForName ("UTF-8"));
    QTextCodec::setCodecForCStrings (QTextCodec::codecForName ("UTF-8"));
#endif
    QTextCodec::setCodecForLocale (QTextCodec::codecForName ("UTF-8"));

    splash.showMessage (QObject::tr ("Initialisations..."),
                        Qt::AlignHCenter | Qt::AlignBottom,
                        Qt::red);
    QCoreApplication::processEvents ();

    // pour avoir les boutons des boîtes de dialogue dans la langue locale (fr par défaut)
    QString loc = (QLocale::system ().name ()).left (2);

    QTranslator qtTranslator;
    if (qtTranslator.load ("qt_" + loc, QLibraryInfo::location (QLibraryInfo::TranslationsPath))) {
        app.installTranslator (&qtTranslator);
    }
    else {
        qDebug () << QObject::tr ("La traduction de Qt n'est pas disponible");
    }

    // pour avoir l'appli localisée
    QTranslator appTranslator;
    if (appTranslator.load ("mcroises_" + loc, ":/translate/")) {
        app.installTranslator (&appTranslator);
    }
    else {
        qDebug () << QObject::tr ("La traduction de l'application n'est pas disponible");
    }

    test ();

    initDirs ();

    splash.showMessage (QObject::tr ("%1 version %2\nChargement des dictionnaires...")
                        .arg (app.applicationName ())
                        .arg	 (app.applicationVersion ()),
                        Qt::AlignHCenter | Qt::AlignBottom,
                        Qt::blue);
    QCoreApplication::processEvents ();

    Widget w;
    w.show ();
    splash.finish (&w);

    QApplication::restoreOverrideCursor ();

    return app.exec ();
}
//---------------
void initDirs ()
{
    QDir appHomeDir (Config::appHomePath ());
    if (not appHomeDir.exists ())
    {
        QDir homeDir (Config::userHomePath ());
        homeDir.mkdir (".mcroises");
    }

    QFile annexeDico (Config::annexeDico ());
    if (not annexeDico.exists ())
    {
        annexeDico.open (QIODevice::WriteOnly);
        annexeDico.write ("");
        annexeDico.close ();
    }

    QDir refDicosDir (Config::refDicsPath ());
    if (not refDicosDir.exists ())
    {
        qDebug () << "Création de refdics";
        QDir appHomeDir (Config::appHomePath ());
        appHomeDir.mkdir ("refdics");
    }

    QString fromPath (Config::officialRefDicosPath ());
    QString toPath (Config::refDicsPath ());

    QDir fromDir (fromPath);
    fromDir.setNameFilters (QStringList() << "*.dic");
    QStringList filesToCopy (fromDir.entryList());

    foreach (const QString & name, filesToCopy)
    {
        QString fileFromName (fromPath + name);
        QString fileToName (toPath + name);

        if (QFile::exists (fileToName))
        {
            QFile::setPermissions (fileToName, QFile::ReadOwner | QFile::WriteOwner);
            QFile::remove (fileToName);
        }

        QFile::copy (fileFromName, fileToName);
        QFile::setPermissions (fileToName, QFile::ReadOwner | QFile::WriteOwner);
        qDebug () << QObject::tr ("Copie de %1 vers %2 effectuée").arg (fileFromName).arg (fileToName);
    }

}
